<?php

namespace App\Services;
use Emmetltd\AliyunCore\Api\imm\Request\V20170906 as Imm;
use Emmetltd\AliyunCore\DefaultAcsClient;
use Emmetltd\AliyunCore\Profile\DefaultProfile;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class ImmService
{

    public static function docToPng(string $SrcUri,string $TgtUri){
        $iClientProfile = DefaultProfile::getProfile(
            config('site.regionId'),        # 项目所在地域ID。
            config('site.accessKeyId'),            # RAM用户的AccessKey ID。
            config('site.accessSecret')           # RAM用户的AccessKey Secret。
        );
//        try{
            $client = new DefaultAcsClient($iClientProfile);
            //项目名称。请确保已创建该项目且项目类型为文档标准型（DocStarter）。
            //创建文档转换任务。
            $request = new Imm\CreateOfficeConversionTaskRequest();
            $request->setProject(config('site.project_name'));
            //设置待转换文件的OSS URI。
            $request->setSrcUri($SrcUri);
            //设置文件输出格式为vector。
            $request->setTgtType("png");
            //设置转换后文件的输出路径。
            $request->setTgtUri($TgtUri);
            $response = $client->getAcsResponse($request);

//      获取文档转换任务结果。
//      设置最大轮询次数为30。
            $maxRetryCount = 100;
//      设置每次轮询的时间间隔为1秒。
            $retryDelay = 1;
            $request = new Imm\GetOfficeConversionTaskRequest();
            $request->setTaskId($response->TaskId);
            $request->setProject(config('site.project_name'));
            while($maxRetryCount--){
                sleep($retryDelay);
                $response = $client->getAcsResponse($request);
                if($response->Status != 'Running') break;
            }
//        }catch (\Exception $e){
//            Log::info('ret:'.$e->getMessage());
//        }
    }

    public function ImCurl($data)
    {
        $data  = json_encode($data);
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output,true);
    }

    /**生成水印
     * @param $params
     * @return void
     */
    public static function makeWater($url,$title){
        $backgroud = public_path('/static/emtry.png');
        $handler =  \think\Image::open($backgroud);
        $uuid = uniqid();
        $date = date('Y-m-d',time());
        $local = [49, 79];
        $thumb =  \think\Image::open($url);
        $thumb->thumb(195, 590);
        if(!is_dir(public_path('/upload/thumb/'))){
            mkdir(public_path('/upload/thumb/'),0777,true);
        }
        $thumb->save(public_path('/upload/thumb/'.$uuid.'.jpg'));
        $handler->text($title, public_path('/static/song.ttf'), 13,'#464646',[70,23]);
        $handler->text('word格式/A4打印/内容随意修改', public_path('static/song.ttf'), 7,'#00000000',[70,48]);
        $handler->water(public_path('/upload/thumb/'.$uuid.'.jpg'), $local); // $path 图片本地路径  $local 数组 [$x, $y];
        $handler->save(public_path('/upload/thumb/'.$uuid.'.jpg'));
        return '/upload/thumb/'.$uuid.'.jpg';
    }

    /**获取文件大小
     * @param $num
     * @return string
     */
    public static function getFilesize($num){
        $p = 0;
        $format='bytes';
        if($num>0 && $num<1024){
            $p = 0;
            return number_format($num).' '.$format;
        }
        if($num>=1024 && $num<pow(1024, 2)){
            $p = 1;
            $format = 'KB';
        }
        if ($num>=pow(1024, 2) && $num<pow(1024, 3)) {
            $p = 2;
            $format = 'MB';
        }
        if ($num>=pow(1024, 3) && $num<pow(1024, 4)) {
            $p = 3;
            $format = 'GB';
        }
        if ($num>=pow(1024, 4) && $num<pow(1024, 5)) {
            $p = 3;
            $format = 'TB';
        }
        $num /= pow(1024, $p);
        return number_format($num, 2).' '.$format;
    }


}
